package com.bootdo.staticmask.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.staticmask.domain.MaskJobDO;
import com.bootdo.staticmask.service.MaskJobService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 脱敏任务
 * 
 * @author lkg
 * @email lkggod@163.com
 * @date 2018-04-25 11:27:35
 */
 
@Controller
@RequestMapping("/staticmask/maskJob")
public class MaskJobController {

	private final MaskJobService maskJobService;

	@Autowired
	public MaskJobController(MaskJobService maskJobService) {
		this.maskJobService = maskJobService;
	}


	@GetMapping()
	@RequiresPermissions("staticmask:maskJob:maskJob")
	String MaskJob(){
	    return "staticmask/maskJob/maskJob";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("staticmask:maskJob:maskJob")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MaskJobDO> maskJobList = maskJobService.list(query);
		int total = maskJobService.count(query);
		PageUtils pageUtils = new PageUtils(maskJobList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("staticmask:maskJob:add")
	String add(){
	    return "staticmask/maskJob/add";
	}

	@GetMapping("/edit/{jobId}")
	@RequiresPermissions("staticmask:maskJob:edit")
	String edit(@PathVariable("jobId") Integer jobId,Model model){
		MaskJobDO maskJob = maskJobService.get(jobId);
		model.addAttribute("maskJob", maskJob);
	    return "staticmask/maskJob/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("staticmask:maskJob:add")
	public R save( MaskJobDO maskJob){
		maskJob.setJobCreatetime(new Date());
		if(maskJobService.save(maskJob)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("staticmask:maskJob:edit")
	public R update( MaskJobDO maskJob){
		maskJobService.update(maskJob);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskJob:remove")
	public R remove( Integer jobId){
		if(maskJobService.remove(jobId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskJob:remove")
	public R remove(@RequestParam("ids[]") Integer[] jobIds){
		maskJobService.batchRemove(jobIds);
		return R.ok();
	}
	
}
